<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" :label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin: 10px 0px">
        <t-col :span="11" class="query-form-inline" >
          <t-form-item label="指南名称" name="guideName">
            <t-input
              v-model="queryParams.guideName"
              clearable
              placeholder="指南名称"
            />
          </t-form-item>
          <t-form-item label="所属年度" name="guideYear">
            <t-date-picker v-model="queryParams.guideYear" mode="year" clearable/>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="dialog.header='新建指南';dialog.objId=0;dialog.open=true" variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        新建指南
      </t-button>
    </div>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
    >
      <template #op="{ row }">
        <t-button @click="dialog.header='修改指南';dialog.objId=row.id;dialog.open = true;" size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          修改
        </t-button>
        <t-button theme="danger" @click="handleDel(row)"  size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
      </template>
      <template #sysAttMainEntity="{ row }">
        <a class="t-button-link" v-for="(item,index) in row.sysAttMainEntity" :key="index" target="_blank" :href="item.fullFilePath">
          {{item.fileName}}
        </a>
      </template>
    </t-table>
    <add-edit-guide @reload="getList()"  :objId="dialog.objId" :visible.sync="dialog.open" v-if="dialog.open"></add-edit-guide>
  </div>
</template>

<script>
import AddEditGuide from './components/add-edit-guide.vue'

export default {
  name: "project-guide",
  components:{
    AddEditGuide
  },
  data(){
    return {
      columns:[
        // {
        //   colKey: "id",
        //   title: "指南编号",
        //   align: "center"
        // },
        {
          colKey: "guideName",
          title: "指南名称",
          align: "center"
        },
        {
          colKey: "guideYear",
          title: "所属年度",
          align: "center"
        },
        {
          colKey: "note",
          title: "备注",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "sysAttMainEntity",
          title: "附件",
          align: "center",
          width:120
        },
        {
          colKey: "createTime",
          title: "创建时间",
          align: "center"
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          fixed: 'right',
        },
      ],
      dialog:{
        open:false,
        header:"",
        objId:0,
      },
      data:[],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        guideName:"",
        guideYear:"",
        note: "",
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created() {
    this.getList();
  },
  methods:{
    getList(){
      this.dataLoading = true;
      this.$api.kjxm.project.guideList(this.queryParams).then(
        (res) => {
          this.data = res.rows;
          this.pagination.total = res.total;
          this.dataLoading = false;
        }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    handleDel(row){
      const confirmDia = this.$dialog.confirm({
        header:"系统提示",
        body:`是否确认删除指南名称为"${row.guideName}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.kjxm.project.guideDel(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
      })
    },
    handleDownload(item){
      // 利用a标签自定义下载文件名
      // const link = document.createElement('a')
      // axios.post(item.fullFilePath, {}, {responseType: 'arraybuffer'}).then(res => {
      //   // 创建Blob对象，设置文件类型
      //   const blob = new Blob([res.data], {type: item.fileFormat})
      //   const objectUrl = URL.createObjectURL(blob) // 创建URL
      //   link.href = objectUrl
      //   link.download = `${item.fileName}` // 自定义文件名
      //   link.click() // 下载文件
      //   URL.revokeObjectURL(objectUrl); // 释放内存
      // })
    }
  }
}
</script>

<style scoped lang="less">
.op_groups{
  display: flex;

}
</style>
